/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AppViewContentRalationDao;
import com.scpii.universal.hibernate.entity.AppViewContentRalationTab;

/**
 * genericWeb
 * @author weny
 * 2012-7-6上午10:16:50
 */
@Repository
public class AppViewContentRalationDaoImpl extends BaseDaoImpl<AppViewContentRalationTab, Integer>  implements AppViewContentRalationDao {

	@Override
	public void delete(Integer id) {
		String hql = "update AppViewContentRalationTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intViewContId=? ";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
					.setParameter(2, id).executeUpdate();
	}

	@Override
	public void deleteByViewId(int viewId) {
		String hql = "update AppViewContentRalationTab a " +
				"set a.intDeleteSign=?,a.datUpdateDatetime=? where a.intViewId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, viewId)
				.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppViewContentRalationTab> getRelationsByViewId(int viewId,
			String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intViewId=?"
				+ " order by entity.intSortOrder desc,entity.intViewContId asc";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1,
						viewId).list();
	}

	@Override
	public Long getBindingContentCount(Integer intContentId) {
		String hql = "select count(*) from AppViewContentRalationTab a where a.intDeleteSign=? and a.intContentId=?";
		return (Long) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql.toString())
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, intContentId).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppViewContentRalationTab> getRelationsByContId(
			int intContentId, String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intContentId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, intContentId).list();
	}

	@Override
	public AppViewContentRalationTab getRelationInfoById(int intViewContId,
			String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intViewContId=?";
		return (AppViewContentRalationTab) getHibernateTemplate()
				.getSessionFactory().getCurrentSession().createQuery(hql)
				.setParameter(0, intViewContId).uniqueResult();
	}
	
}
